package com.pospi.pospihold.fragment;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.pospi.pospihold.R;
import com.pospi.pospihold.util.Constant;
import com.pospi.pospihold.util.TurnSize;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipFragment extends BaseFragment {

    @Bind(R.id.tv_pop)
    TextView tvPop;
    @Bind(R.id.et_vipNumber)
    EditText etVipNumber;
    @Bind(R.id.tv_search)
    TextView tvSearch;
    private Context context;

    private PopupWindow pop;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_vip, container, false);
        ButterKnife.bind(this, view);
        context = getActivity().getApplicationContext();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_pop, R.id.tv_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_pop:
                LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View contentView = mLayoutInflater.inflate(R.layout.vip_pouple_window, null);
                // R.layout.pop为PopupWindow 的布局文件
                pop = new PopupWindow(contentView, TurnSize.dip2px(context, 150), TurnSize.dip2px(context, 180));
                pop.setBackgroundDrawable(new BitmapDrawable());
                // 指定 PopupWindow 的背景
                pop.setFocusable(true);
                pop.showAsDropDown(tvPop);

                LinearLayout phone = (LinearLayout) contentView.findViewById(R.id.ll_phone);
                LinearLayout member = (LinearLayout) contentView.findViewById(R.id.ll_vip_member);
                LinearLayout name = (LinearLayout) contentView.findViewById(R.id.ll_vip_name);
                LinearLayout scan = (LinearLayout) contentView.findViewById(R.id.ll_vip_scan);

                MyClickListener listener = new MyClickListener();
                phone.setOnClickListener(listener);
                member.setOnClickListener(listener);
                name.setOnClickListener(listener);
                scan.setOnClickListener(listener);


                break;
            case R.id.tv_search:
                break;
        }
    }

    class MyClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_phone:
                    tvPop.setText(Constant.VIP_PHONE);
                    break;
                case R.id.ll_vip_member:
                    tvPop.setText(Constant.VIP_NUMBER);
                    break;
                case R.id.ll_vip_name:
                    tvPop.setText(Constant.VIP_NAME);
                    break;
                case R.id.ll_vip_scan:
                    tvPop.setText(Constant.VIP_SCAN);
                    break;

            }
            pop.dismiss();
        }
    }
}
